<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<c:if test="${not clinic.offlineMode}">
		<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
	</c:if>
		<script type="text/javascript">

			function popdialog(selector, callback) {
				var triggers = $(selector), trigger_event = 'click';

				triggers.each(function() {
					var trigger = $(this), popover = trigger.parent().find('.popover');

					popover.click(function(e) {
						e.stopPropagation();
					});

					trigger.bind(trigger_event, function(e) {
						var trigger = $(this), popover = trigger.parent().find('.popover');

						e.preventDefault();
						e.stopPropagation();

						if (typeof callback === 'function') {
							callback(trigger, popover);
						}

						if (popover.is(':visible')) {
							popover.hide();
							trigger.removeClass('active');
						} else {
							popover.show();
							trigger.addClass('active');

							$('body').one('click', function() {
								popover.hide();
								trigger.removeClass('active');
							});
						}
					});
				});
			}

			function popdialogFocus(trigger, popover) {
				setTimeout(function() {
					popover.find('#username').trigger('focus');
				}, 200);
			}

			$(document).ready(function() {
			<c:if test="${not clinic.offlineMode}">
				var googleMapElement = $("#google-map");

				var lat = googleMapElement.data('latitude');
				var lon = googleMapElement.data('longitude');

				var latlng = new google.maps.LatLng(lat, lon);
				var mapOpts = {
					zoom : 16,
					center : latlng,
					mapTypeId : google.maps.MapTypeId.ROADMAP
				};
				new google.maps.Map(document.getElementById("google-map"), mapOpts);
			</c:if>
				popdialog('.trigger-popover', popdialogFocus);
				
				$("#loginAction").click(function(e) {
					e.preventDefault();
					$.ajax({
						url: '<c:url value="/auth.do" />',
						type: 'POST',
						dataType: 'json',
						data: {
							username: $("#username").val(),
							password: $("#password").val()
						},
						success: function(response) {
							if (response.success) {
								window.location = '<c:url value="/dashboard.do" />';
							} else {
								$("#password").val('');
								$("#password").focus();
								$("#messageOutput")
									.html(response.message)
									.fadeIn()
									.delay(3000)
									.slideUp();
							}
						}
					});
				});
				
				setupSchedule(${startHour}, '<c:url value="/schedule" />', true);
				
			    $("#dp").datepicker({
			    	beforeShowDay: mondayOnly,
			        buttonImage: 'resources/images/calendar.png',
			        buttonText: "Ver outra semana",
			        buttonImageOnly: true,
			        changeMonth: true,
			        changeYear: true,
			        onSelect: function(date) {
			        	setHeaders('<c:url value="/schedule" />', date);
			        },
			        showOn: 'both'
			     });
			});
		</script>